#pragma once

#include <windows.h>
#include <sql.h>
#include <sqlext.h>
#include <stdio.h>
#include <conio.h>
#include <tchar.h>
#include <stdlib.h>
#include <sal.h>

#include "../CommonUtil/messages.h"
#include <vector>
using namespace std;

class CBusinessLogic
{
public:
	~CBusinessLogic(void);

public:
	static CBusinessLogic& GetInstance();

public: //services provided
	bool UserRegister(const char* pAccount, const char* pPasswd);
	bool UserAuth(const char* pAccount, const char* pPasswd);
	bool AirLineRegister(const char* pAccount, const char* pPasswd);
	bool AirLineAuth(const char* pAccount, const char* pPasswd);

	void UserSelRoute(IN MSG_ROUTE_SEL& sel, OUT vector<ROUT_ENTRY>& vecRes);
	bool UserOrderRoute(const char* szAccount, int nRouteId, CABIN_TYPE nCabTp);
	void ViewOrders(bool bCus, const char* szAccount, vector<ORDER_ENTRY>& vecRes);
	bool CancelAnOrder(bool bCus, const char* szName, int nOrderId);
	bool AddRoute(ROUT_ENTRY& route);
	void ViewRoutes(const char* szAirline, OUT vector<ROUT_ENTRY>& vecRes);

private:
	CBusinessLogic(void);

//Database related functions
public:
	bool ConnDataBase(const char* szDSN);

private:
public:
	bool ExecuteSQL(const char* szSql);
	SQLRETURN GetResultset();
	void DescribeColumns();

	struct ColDescription
	{
		SQLSMALLINT colNumber;
		SQLCHAR colName[80];
		SQLSMALLINT nameLen;
		SQLSMALLINT dataType;
		SQLULEN colSize;
		SQLSMALLINT decimalDigits;
		SQLSMALLINT nullable;
	};
	SQLRETURN Describe(ColDescription& c);

	SQLRETURN GetColData(int colnum, string& str);

	bool GetFieldByColName(IN vector<string>& entry, 
		IN const char* szColName, OUT string& strRes);

private://Database related members
	SQLHANDLE m_envHandle;
	SQLHANDLE m_conHandle;
	SQLHANDLE m_stmtHandle;
	vector<ColDescription> cols;
	vector<vector<string>> colData;
};

